<template>
	<div class="page">
		
		<CAccountLoginView
			:data="params"
			@on-action="click2Action">
		</CAccountLoginView>
		
	</div>
</template>

<script>
import CAccountLoginView from '@/pages/account/view/CAccountLoginView'

import AccountService from '@/services/AccountService.js'
import Security from '@/libs/security.js'

const $accountService = AccountService();

export default {
	components: {
		CAccountLoginView
	},
	data() {
		return {
				params: { //登录参数，用户名、密码
					isSubmitLoading: false,
					username: null,
					pwd: null,
					isRem: false
				}
			}
		},
		
		mounted() {//加载本地存储
			if(localStorage.getItem('account.username')) {
				this.params.username = localStorage.getItem('account.username');
			}
			
			if(localStorage.getItem('account.pwd')) {
				this.params.pwd = localStorage.getItem('account.pwd');
			}
			
			if(localStorage.getItem('account.isRem')) {
				this.params.isRem = localStorage.getItem('account.isRem') == 'true';
			}
		},
		
		methods: {
			click2Action(type, data) {
				if(type == 'login') {
					this.login();
				} else if(type == 'forget-pwd') {
					this.$router.push({
						name: 'accountFPwd2Phone'
					})
				}
			},
			//登录
			login() {
				var $this = this;
				var username = this.params.username;
				var password = this.params.pwd;
				this.params.isSubmitLoading = true;
				//通过给定的ID来发送请求
				$accountService.login(username, password)
				.then(function(response) {
					var result = response.data;
					var rescode = result.rescode;
					var resmsg = result.msg;
					if (rescode == 100) {
						var data = result.data;
						//存储数据
						localStorage.setItem('account.username', $this.params.username);
						localStorage.setItem('account.isRem', $this.params.isRem);
						if($this.params.isRem == true) {
							localStorage.setItem('account.pwd', Security.encrypt($this.params.pwd))
						} else {
							localStorage.removeItem('account.pwd');
						}
						//更新全局变量
						$this.$store.commit('setAccount', data);
						
						if(data.roleType == 1 || data.roleType == 2) {
							$this.$store.commit('setMenu', {
								active: 'home',
								opens: []
							});
							
							$this.$router.push({
								name: 'home'
							});
						} else if(data.roleType == 3) {
							$this.$store.commit('setMenu', {
								active: 'user/atags/group',
								opens: ['user-tag-menus']
							});
							
							$this.$router.push({
								name: 'atagGroup'
							});
						} else if(data.roleType == 4) {
							$this.$store.commit('setMenu', {
								active: 'works/worksEdit',
								opens: ['works-menus']
							});
							
							$this.$router.push({
								name: 'worksEdit'
							});
						}
					} else {
						$this.$Message.error(resmsg);
					}
					$this.params.isSubmitLoading = false;
				}, function(error) {
					$this.params.isSubmitLoading = false;
				});
			}
		}
	}
	</script>

<style scoped>
.page {
	background: url(../../assets/login_background.png);
	background-repeat: no-repeat;
	background-size: 100% 100%;
	width: 100%;
	overflow: auto;
	top: 84px;
	position: absolute;
	z-index: 10;
	bottom: 100px;
	display: flex;
	align-items: center;
}

</style>